package com.mojo.quartz;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;

import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Spring Schedule实现定时任务示例
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/3/11 10:55
 */
@Configuration
//@EnableScheduling
@Slf4j
public class SpringScheduleDemo {
    AtomicInteger count = new AtomicInteger(0);

    /**
     * cron表达式设置执行周期
     */
    @Schedules({@Scheduled(cron = "0/1 * * * * ?")})
    @SneakyThrows
    public void schedule() {
        log.info("schedule current time:{},thread name:{}", LocalDateTime.now(), Thread.currentThread().getName());
        log.info(">>>count>>>:{}", count);
        if (count.get() == 2) {
            Thread.sleep(2000);
        }
        //count.getAndDecrement();
    }

    /**
     * cron表达式设置执行周期
     */
    //@Scheduled(fixedRate = 1000)
    @SneakyThrows
    public void scheduleFixedRate() {
        log.info("scheduleFixedRate current time:{},thread name:{}", LocalDateTime.now(), Thread.currentThread().getName());
        //log.info(">>>count:{}", count);
        if (count.get() == 2) {
            Thread.sleep(2000);
            int i = 1 / 0;
        }
        count.getAndIncrement();
    }

}
